package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.CCategory;
import com.ruoyi.system.domain.CNews;
import com.ruoyi.system.mapper.CNewsMapper;
import com.ruoyi.system.service.ICNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 信息资讯Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-05-22
 */
@Service
public class CNewsServiceImpl extends ServiceImpl<CNewsMapper, CNews> implements ICNewsService
{
    @Autowired
    private CNewsMapper cNewsMapper;

    /**
     * 查询信息资讯
     * 
     * @param id 信息资讯ID
     * @return 信息资讯
     */
    @Override
    public CNews selectCNewsById(Long id)
    {
        return cNewsMapper.selectCNewsById(id);
    }

    /**
     * 查询信息资讯列表
     * 
     * @param cNews 信息资讯
     * @return 信息资讯
     */
    @Override
    public List<CNews> selectCNewsList(CNews cNews)
    {
        return cNewsMapper.selectCNewsList(cNews);
    }

    /**
     * 新增信息资讯
     * 
     * @param cNews 信息资讯
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCNews(CNews cNews)
    {
        cNews.setNewsContent(cNews.getNewsContent().replace("<o:p></o:p>",""));
        cNews.setCreateTime(DateUtils.getNowDate());
        int rows = cNewsMapper.insertCNews(cNews);
        insertCCategory(cNews);
        return rows;
    }

    /**
     * 修改信息资讯
     * 
     * @param cNews 信息资讯
     * @return 结果
     */
    @Transactional
    @Override
    public boolean updateCNews(CNews cNews)
    {
        //cNews.setUpdateTime(DateUtils.getNowDate());
        cNews.setNewsContent(cNews.getNewsContent().replace("<o:p></o:p>",""));
        cNewsMapper.deleteCCategoryById(cNews.getId());
        insertCCategory(cNews);
        //return cNewsMapper.updateCNews(cNews);
        return this.saveOrUpdate(cNews);
    }

    /**
     * 删除信息资讯对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Transactional
    @Override
    public boolean deleteCNewsByIds(String ids)
    {
        cNewsMapper.deleteCCategoryByIds(Convert.toStrArray(ids));
        //return cNewsMapper.deleteCNewsByIds(Convert.toStrArray(ids));
        return this.removeByIds(Arrays.asList(Convert.toStrArray(ids)));
    }

    /**
     * 删除信息资讯信息
     * 
     * @param id 信息资讯ID
     * @return 结果
     */
    @Override
    public boolean deleteCNewsById(Long id)
    {
        cNewsMapper.deleteCCategoryById(id);
        //return cNewsMapper.deleteCNewsById(id);
        return this.removeById(id);
    }

    /**
     * 新增资讯类别信息
     * 
     * @param cNews 信息资讯对象
     */
    public void insertCCategory(CNews cNews)
    {
        List<CCategory> cCategoryList = cNews.getCCategoryList();
        Long id = cNews.getId();
        if (StringUtils.isNotNull(cCategoryList))
        {
            List<CCategory> list = new ArrayList<CCategory>();
            for (CCategory cCategory : cCategoryList)
            {
                cCategory.setId(id);
                list.add(cCategory);
            }
            if (list.size() > 0)
            {
                cNewsMapper.batchCCategory(list);
            }
        }
    }
}
